package com.bitcoin.scheduler.web.rest;

import com.bitcoin.scheduler.service.BlockccService;
import com.codahale.metrics.annotation.Timed;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.concurrent.ExecutionException;

/**
 * Controller for view and managing Log Level at runtime.
 */
@RestController
@RequestMapping("/kline")
public class KlineResource {

    @Autowired
    private BlockccService blockccService;

    @PostMapping("/sync/{market}/{symbol}/{day}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @Timed
    @Transactional
    public Integer sync(@PathVariable String market, @PathVariable String symbol, @PathVariable String day) throws ParseException, ExecutionException, InterruptedException {
        Date date = DateUtils.parseDate(day, "yyyyMMdd");
        Long days = Duration.between(Instant.ofEpochSecond(date.getTime() / 1000), Instant.now()).toDays();
        return blockccService.syncData(market, symbol, days.intValue() / 3 + 1);
    }
}
